<template>
  <el-upload
      class="uploader"
      :action="uploadPath"
      name="File"
      ref="upload"
      :drag="true"
      :multiple="false"
      :headers="headers"
      :accept="accept"
      :show-file-list="false"
      :disabled="loading"
      :auto-upload="autoUpload"
      :on-change="handleChange"
      :on-success="handleSuccess"
      :on-error="handleError"
      :on-exceed="handleExceed"
      :on-progress="handleProgress"
      :before-upload="beforeUpload">
    <div class="loading" v-if="loading">
      <i class="el-icon-loading uploader-icon"></i>
    </div>
    <img v-if="image" :src="image" class="avatar">
    <i v-else class="el-icon-plus uploader-icon"></i>
  </el-upload>
</template>

<script>
import setting from '@/config/setting'
import { envDistinguishDomain } from '@/utils/common'

export default {
  name: 'img-single',
  props: {
    action: {
      type: String,
      default: ''
    },
    accept: {
      type: String,
      default: 'image/jpeg, image/jpg, image/png'
    },
    size: {
      type: Number,
      default: () => {
        return 1024 * 1024 * 2
      }
    },
    autoUpload: {
      type: Boolean,
      default: false
    },
    defaultImg: {
      type: String,
      default: ''
    }
  },
  data () {
    return {
      image: this.defaultImg,
      uploadPath: '',
      headers: {},
      loading: false,
      status: 0, // 未上传  1:已上传
      result: false
    }
  },
  watch: {
    defaultImg () {
      if (this.defaultImg) {
        this.image = this.defaultImg
      }
    }
  },
  created () {
    this.uploadPath = envDistinguishDomain(this.action)
    this.headers[setting.tokenHeaderName] = setting.takeToken()
  },
  methods: {
    // 文件上传
    upload () {
      let file = this.$refs.upload.uploadFiles
      if (file.length === 0 || this.result) {
        this.$emit('success', this.result, false)
        return false
      }
      this.$refs.upload.submit()
    },
    handleChange (file) {
      console.log('handleChange')
      this.image = URL.createObjectURL(file.raw)
    },
    handleSuccess (res) {
      console.log('handleSuccess')
      this.loading = false
      if (res.code === 200) {
        this.result = res.result
        this.$emit('success', res.result, true)
      } else {
        this.$message.error(res.msg || '图片上传失败')
      }
    },
    beforeUpload (file) {
      let type = file.type || file.raw.type
      let accept = this.accept.split(',')
      if (!accept.includes(type)) {
        this.$message.error('只支持上传' + this.accept + '文件')
        return false
      }
      if (file.size > this.size) {
        this.$message.error('上传文件不可超过' + (this.size / 1024 / 1024) + 'M')
        return false
      }
      return true
    },
    handleError (err) {
      this.loading = false
      this.image = ''
      this.$message.error('文件上传失败：', err)
    },
    handleExceed () {
      this.$message.error('只能选择1个文件')
    },
    handleProgress () {
      this.loading = true
    },
  }
}
</script>

<style lang="scss">
.uploader {
  .el-upload-dragger {
    position: relative;
    width: 165px;
    height: 165px;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .loading {
    position: absolute;
    width: 100%;
    height: 100%;
    z-index: 99;
    left: 0;
    top: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(255, 255, 255, 0.5);
  }

  .uploader-icon {
    font-size: 28px;
    color: #8c939d;
    text-align: center;
  }

  .avatar {
    width: 100%;
    height: 100%;
    display: block;
  }
}
</style>
